//
//  AppDelegate.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/26.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        let userAccount: DBUserAccount? = DBUserAccountViewModel.shareUserAccountViewModel.userAccount
        
//        print("在appDelegate获取账户: \(userAccount)")
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = rootViewController()
        
        window?.rootViewController?.view.backgroundColor = UIColor.grayColor()
        
        window?.makeKeyAndVisible()

        setAppearance()
        
        return true
    }
    
    private func rootViewController() -> UIViewController {
        
        if !DBUserAccountViewModel.shareUserAccountViewModel.isUserLogin {
            
            return DBMainController()
        }
        
        return isNewVersion() ? DBNewFeatureController() : DBWelcomeController()
    }
    
    private func switchMain(isMain: Bool) {
        
        window?.rootViewController = isMain ? DBMainController() : DBWelcomeController()
    }
    
    class func outSwitchMain(isMain: Bool) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        appDelegate.switchMain(isMain)
    }
    
    //设置全局样式
    private func setAppearance() {
        
        let item = UIBarButtonItem.appearance()
        
        item.tintColor = UIColor.orangeColor()
    }

    ///判断版本号
    private func isNewVersion() -> Bool {
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        let sandBoxKey = "sandBoxVersion"
        
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandBoxKey)
        
        let isNew = currentVersion != sandBoxVersion
        
        if isNew {
            
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandBoxKey)
            
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        return isNew
    }
}













